package vn.com.ltv.lo.daos;

import java.util.ArrayList;

import vn.com.ltv.lo.dtos.SetDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SetDAO {
	protected static final String TABLE_NAME = "Sets";
	protected static final String ID = "rowid";
	protected static final String SET_ID = "set_id";
	protected static final String SET_NM = "set_nm";
	protected static final String SET_RT_AVRG = "set_rt_avrg";
	protected static final String RT_NUMB = "rt_numb";
	protected static final String SET_DESC = "set_desc";
	protected static final String SET_ID_THUMG = "set_id_thumb";
	protected static final String SET_ID_IMG = "set_id_img";
	protected static final String SET_PRC = "set_prc";
	protected static final String CTG_ID = "ctg_id";
	protected static String TABLE_CREATION_STRING = "CREATE TABLE "
			+ "\"Set\" (\"rowid\" integer primary key autoincrement , \"set_id\" TEXT DEFAULT (null) ,"
			+ "\"set_nm\" TEXT DEFAULT (null) ,\"set_rt_avrg\" TEXT DEFAULT (null) ,"
			+ "\"rt_numb\" INTEGER DEFAULT (null) ,\"set_desc\" TEXT DEFAULT (null) ,"
			+ "\"set_id_thumg\" TEXT DEFAULT (null) ,\"set_id_img\" TEXT DEFAULT (null) ,"
			+ "\"set_prc\" INTEGER DEFAULT (null) ,\"ctg_id\" TEXT DEFAULT (null) )";
	protected static String TABLE_DROP_STRING = "drop table if exists"
			+ TABLE_NAME + ";";
	private Context context;

	public SetDAO(Context context) {
		this.context = context;
	}

	public ArrayList<SetDTO> getAll() {
		ArrayList<SetDTO> setDTOs = new ArrayList<SetDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, null, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				SetDTO setDTO = this.cursorToSetDTO(cursor);
				setDTOs.add(setDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return setDTOs;
	}

	public SetDTO get(int id) {
		SetDTO setDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			setDTO = this.cursorToSetDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return setDTO;
	}
	
	public SetDTO get(String idSet) {
		SetDTO setDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, SET_ID + "=?",
				new String[] { String.valueOf(idSet) }, null, null, null);
		if (cursor.moveToFirst()) {
			setDTO = this.cursorToSetDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return setDTO;
	}
	
	public ArrayList<SetDTO> getAllByCategoryId(int id) {
		ArrayList<SetDTO> foodDTOs = new ArrayList<SetDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, CTG_ID + "=?", new String[] { String.valueOf(id) },
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				SetDTO setDTO = this.cursorToSetDTO(cursor);
				foodDTOs.add(setDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return foodDTOs;
	}
	
	public ArrayList<SetDTO> getAllByCategoryByName(String name) {
		ArrayList<SetDTO> foodDTOs = new ArrayList<SetDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		String query = "SELECT * FROM "+TABLE_NAME+"  WHERE "+SET_NM+" LIKE '%"+name+"%'";
		Cursor cursor = sQLiteDatabase.rawQuery(query, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				SetDTO setDTO = this.cursorToSetDTO(cursor);
				foodDTOs.add(setDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return foodDTOs;
	}

	public void insert(SetDTO setDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		ContentValues contentValues = this.setDTOToContentValues(setDTO);
		sQLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	/** * @return the number of rows affected */
	public int update(SetDTO setDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		int id = setDTO.getId();
		setDTO.setId(-1);
		ContentValues contentValues = this.setDTOToContentValues(setDTO);
		int rowCount = sQLiteDatabase.update(TABLE_NAME, contentValues, ID
				+ "=?", new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
		return rowCount;
	}

	public void delete(int id) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		sQLiteDatabase.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	private SetDTO cursorToSetDTO(Cursor cursor) {
		SetDTO setDTO = new SetDTO();
//		int id = cursor.getInt(cursor.getColumnIndex(ID));
//		setDTO.setId(id);
		String setId = cursor.getString(cursor.getColumnIndex(SET_ID));
		setDTO.setSetId(setId);
		String setNm = cursor.getString(cursor.getColumnIndex(SET_NM));
		setDTO.setSetNm(setNm);
		float setRtAvrg = cursor.getFloat(cursor.getColumnIndex(SET_RT_AVRG));
		setDTO.setSetRtAvrg(setRtAvrg);
		int rtNumb = cursor.getInt(cursor.getColumnIndex(RT_NUMB));
		setDTO.setRtNumb(rtNumb);
		String setDesc = cursor.getString(cursor.getColumnIndex(SET_DESC));
		setDTO.setSetDesc(setDesc);
		String setIdThumg = cursor.getString(cursor
				.getColumnIndex(SET_ID_THUMG));
		setDTO.setSetIdThumg(setIdThumg);
		String setIdImg = cursor.getString(cursor.getColumnIndex(SET_ID_IMG));
		setDTO.setSetIdImg(setIdImg);
		int setPrc = cursor.getInt(cursor.getColumnIndex(SET_PRC));
		setDTO.setSetPrc(setPrc);
		String ctg_id = cursor.getString(cursor.getColumnIndex(CTG_ID));
		setDTO.setCtg_id(ctg_id);
		return setDTO;
	}

	private ContentValues setDTOToContentValues(SetDTO setDTO) {
		ContentValues contentValues = new ContentValues();
		int id = setDTO.getId();
		if (id >= 0) {
			contentValues.put(ID, id);
		}
		String setId = setDTO.getSetId();
		if (setId != null) {
			contentValues.put(SET_ID, setId);
		}
		String setNm = setDTO.getSetNm();
		if (setNm != null) {
			contentValues.put(SET_NM, setNm);
		}
		float setRtAvrg = setDTO.getSetRtAvrg();
		if (setRtAvrg >= 0) {
			contentValues.put(SET_RT_AVRG, setRtAvrg);
		}
		int rtNumb = setDTO.getRtNumb();
		if (rtNumb >= 0) {
			contentValues.put(RT_NUMB, rtNumb);
		}
		String setDesc = setDTO.getSetDesc();
		if (setDesc != null) {
			contentValues.put(SET_DESC, setDesc);
		}
		String setIdThumg = setDTO.getSetIdThumg();
		if (setIdThumg != null) {
			contentValues.put(SET_ID_THUMG, setIdThumg);
		}
		String setIdImg = setDTO.getSetIdImg();
		if (setIdImg != null) {
			contentValues.put(SET_ID_IMG, setIdImg);
		}
		int setPrc = setDTO.getSetPrc();
		if (setPrc >= 0) {
			contentValues.put(SET_PRC, setPrc);
		}
		
		String ctg_id = setDTO.getCtg_id();
		if (ctg_id != null) {
			contentValues.put(CTG_ID, setIdImg);
		}
		return contentValues;
	}
}